<?php
class ControllerMyplanMyplanhome extends Controller{
	public function index(){
		
		$query=array();
		
		$query["user_id"]=$this->user->getId();
	
		if(isset($this->request->get["status"])){
			$query["status"]=$this->request->get["status"];
		}else{
			$query["status"]="1";
		}
		
		$limit=10;
		
		if(isset($this->request->get["page"])){
			$page=$this->request->get["page"];
		}else{
			$page=1;
		}
		
		$query["start"]=($page-1)*$limit;
		$query["limit"]=$limit;
	
		$this->load->model("myplan/plan");
		
		$this->data["myplans"]=$this->model_myplan_plan->getAllPlans($query);
		$this->data['status']=$this->model_myplan_plan->getStatus($query["status"]);
		$this->data['allstatus']=$this->model_myplan_plan->getAllStatus();
		$this->data["plan_types"]=$this->model_myplan_plan->getPlanTypes();
		$plan_total=$this->model_myplan_plan->getTotalPlans($query);
		
		$this->data["action_giveup"]=$this->url->link("myplan/myplanhome/dealaction")."&action=5"."&plan_id=";
		$this->data["action_done"]=$this->url->link("myplan/myplanhome/dealaction")."&action=4"."&plan_id=";
		$this->data["action_doing"]=$this->url->link("myplan/myplanhome/dealaction")."&action=1"."&plan_id=";
		$this->data["action_plan"]=$this->url->link("myplan/myplanhome/dealaction")."&action=2"."&plan_id=";
		$this->data["action_pause"]=$this->url->link("myplan/myplanhome/dealaction")."&action=3"."&plan_id=";
		
		$this->data["link_status"]=$this->url->link("myplan/myplanhome")."&status=";
		
		$this->data["addplan"]=$this->url->link("myplan/addplan");
		$this->data["viewplan"]=$this->url->link("myplan/viewplan")."&plan_id=";
		
		//分页
		$pagination = new Pagination();
		$pagination->total = $plan_total;
		$pagination->page = $page;
		$pagination->limit = $limit;
		$pagination->text = "P";
		$pagination->url = $this->url->link('myplan/myplanhome',  $url . '&page={page}')."&status=".$query["status"];
			
		$this->data['pagination'] = $pagination->render();
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/myplan/myplanhome.tpl')) 		            {
				$this->template = $this->config->get('config_template') . '/template/myplan/myplanhome.tpl';
			} else {
				$this->template = 'default/template/myplan/myplanhome.tpl';
			}
			
			$this->children = array(
			'myplancommon/footer',
			'myplancommon/header'
			);
											
			$this->response->setOutput($this->render());
	}	
	
	public function logout(){
		$this->user->logout();
		$this->redirect($this->url->link("myplan/login"));
	}
	
	public function dealaction(){
		
		if($this->request->get["action"]!=""&&isset($this->request->get["action"])){
			$this->load->model("myplan/plan");
			
			$data=$this->request->get;
			$data["user_id"]=$this->user->getId();
			
			$this->model_myplan_plan->updatePlan($data);
			
			$this->session->data["success"]="标记成功.";
			
			$this->redirect($this->url->link("myplan/myplanhome")."&status=".$this->request->get["action"]);
		}
		
		$this->index();
	}
}
?>